package com.lap.auth.adapter.driven.web.role;

import com.lap.auth.application.role.RoleMenuAppService;
import com.lap.auth.application.role.command.BindRoleMenusCmd;
import com.lap.watchdog.starter.annotation.RepeatSubmit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "角色授权菜单管理", description = "提供角色授权菜单，角色已绑定菜单查看")
@RequiredArgsConstructor
@RestController
@RequestMapping("api/v1/roles")
public class RoleMenuController {

  private final RoleMenuAppService roleMenuAppService;

  @Operation(summary = "获取角色已绑定菜单")
  @GetMapping("{roleId}/menus")
  public List<Integer> getRoleMenus(@PathVariable Integer roleId) {
    return roleMenuAppService.getRoleMenusByRoleId(roleId);
  }

  @Operation(summary = "绑定菜单到角色")
  @RepeatSubmit
  @PutMapping("{roleId}/menus")
  public void bindMenus(@PathVariable Integer roleId, @RequestBody @Valid BindRoleMenusCmd cmd) {
    roleMenuAppService.replaceMenus(roleId, cmd.getMenuIds());
  }
}
